from django.db import models

# Create your models here.


class BookInfoManager(models.Manager):

    """自定义models的manage管理器"""

    # 自定义all方法
    def all(self):
        # super是调用父类的方法，相当于是在父类的查询的合集上使用
        return super().all().filter(isDelete=False)

    # 自定义一个添加书籍记录的静态方法
    def addboook(self, bookname, bookdate):
        book = self.model()
        book.book_title = bookname
        book.book_date = bookdate
        book.save()


# 定义书本信息

class BookInfo(models.Model):

    book_title = models.CharField(max_length=20)
    book_date = models.DateField()
    read_num = models.IntegerField(default=0)
    book_comment = models.IntegerField(default=0)
    isDelete = models.BooleanField(default=False)

    # 自定义输出效果
    def __str__(self):
        return "<%s: %s, %d, %d, %s>"%(self.book_title, self.book_date, self.read_num, self.book_comment, self.isDelete)

    # 新建一个自定义管理器的属性，一旦定义，系统将不再产生objects管理器
    bookmanager = BookInfoManager()



# 定义书本任人物模型信息
class PersonInfo(models.Model):
    person_name = models.CharField(max_length=30)
    # person_password = models.CharField(max_length=16)
    isDelete = models.BooleanField(default=False)
    # 用户信息描述，数据库中的字段可以为空，后台管理页面中不能为空，为必填字段
    person_comment = models.CharField(max_length=200, null=True, blank=False)
    # 添加一个图书名称的外键
    hbook_name = models.ForeignKey('BookInfo', on_delete=models.CASCADE, default=None)

    # 自定义输出效果
    def __str__(self):
        return "<%s: %s, %s>" % (self.person_name, self.person_comment, self.isDelete)


from django import forms
class MessageBoardForm(forms.Form):
    title = forms.CharField(max_length=100, min_length=8, label="标题")
    content = forms.CharField(widget=forms.Textarea)
    email = forms.EmailField()
